package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.*;
import com.rzico.basics.mapper.TurntableRecordMapper;
import com.rzico.basics.model.PrizeVo;
import com.rzico.basics.model.TurntableRecordVo;
import com.rzico.core.service.SysSequenceService;
import com.rzico.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * <pre>
 * 营销活动记录业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class TurntableRecordService extends BaseServiceImpl<TurntableRecord, String> {

    @Autowired
    private TurntableRecordMapper turntableRecordMapper;

    @Autowired
    private TurntablePrizeService turntablePrizeService;

    @Autowired
    private CouponCodeService couponCodeService;

    @Autowired
    private SysSequenceService sysSequenceService;

    @Override
    public BaseMapper<TurntableRecord, String> getMapper() {
        return turntableRecordMapper;
    }

    public List<PrizeVo> findByMemberId(Long memberId, Long turntableId) {
        List<PrizeVo> prizeVos = new ArrayList<>();
        List<TurntableRecord> records = turntableRecordMapper.findByMemberId(memberId, turntableId);
        for (TurntableRecord record : records) {
            TurntablePrize prize = turntablePrizeService.findById(record.getPrizeId());
            if (prize == null) {
                continue;
            }
            // 判断 奖品类型是否为 优惠券
            if ("1".equals(prize.getPrizeType()) && prize.getCoupon() != null) {
                prizeVos.add(PrizeVo.format(prize.getCoupon()));
            }
        }
        return prizeVos;
    }

    /**
     * 查询 当前会员 可用次数
     *
     * @param turntable 大转盘
     * @param memberId  会员ID
     */
    public int allowAttend(Turntable turntable, Long memberId) {
        String fqcy = turntable.getFqcy();
        int start = 0, end = 0, day = 0, count = 0;
        switch (fqcy) {
            case "1":
                day = Integer.parseInt(DateUtils.getDateStr(Calendar.getInstance(Locale.CHINA).getTime(), 1));
                break;
            case "2":
                start = Integer.parseInt(DateUtils.getCurrentWeekBegin("yyyyMMdd"));
                end = Integer.parseInt(DateUtils.getCurrentWeekEnd("yyyyMMdd"));
                break;
            case "3":
                start = Integer.parseInt(DateUtils.getCurrentMonthBegin("yyyyMMdd"));
                end = Integer.parseInt(DateUtils.getCurrentMonthEnd("yyyyMMdd"));
                break;
            default:
                System.out.println("未知周期类型--->" + fqcy);
        }
        count = turntableRecordMapper.findNumbyMemberId(turntable.getId(), memberId, day, start, end);
        return turntable.getPerAttendMax() - count;
    }

    /**
     * 查询 当前是否存在未领取的奖品
     *
     * @param turntableId 大转盘ID
     * @param memberId    会员ID
     */
    public TurntableRecord findByisDraw(Long turntableId, Long memberId) {
        return turntableRecordMapper.findByisDraw(turntableId, memberId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void drawLuckSubmit(TurntableRecord record, Member current) {
        if (record.getIsDraw() == 1){ return;}
        if(record.getPrizeId()!=null){
            Long prizeId = record.getPrizeId();
            TurntablePrize prize = turntablePrizeService.findById(prizeId);
            // 进行发放优惠券操作
            String sn = sysSequenceService.generate("couponCode");
            CouponCode couponCode = couponCodeService.create(prize.getCoupon(), current, sn, 1);
            record.setRelationId(couponCode.getId());
        }
        record.setIsDraw(1);
        super.updateByPrimaryKey(record);
    }

    /**
     * 获取 当前活动最新的二十条记录
     */
    public List<TurntableRecordVo> findCurrentRecord(Long turntableId) {
        return turntableRecordMapper.findCurrentRecord(turntableId);
    }

}
